package com.jinmdz.app.dao.dao.provider;

import com.jinmdz.app.dao.model.cremation.SearchTakeAshData;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import static com.jinmdz.fmis.common.util.DataUtil.valid;

public class TakeAshProvider {

    /*
     *
     * 待取灰列表
     * */
    public String listTakeAshs(@Param("data") SearchTakeAshData data) {

        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" funeral_dead.business_code,dead_name,dead_gender_code,dead_age,funeral_dead.certificate_no");
                builder.append(" ,transport_place,arrived_time,benefit_district_code");
                builder.append(" ,family_member.member_name,family_agent.agent_unit_name as agent_unit,family_agent.agent_name");
                builder.append(" ,cremation.cremation_no ");
                SELECT(builder.toString());
                FROM(" funeral_dead ");
                JOIN(" family_member on family_member.business_code = funeral_dead.business_code ");
                LEFT_OUTER_JOIN(" family_agent on family_agent.business_code = funeral_dead.business_code and family_agent.deleted = 0  ");
                LEFT_OUTER_JOIN(" cremation on cremation.business_code = funeral_dead.business_code and cremation.deleted = 0 and cremation.as_active = 1");
                WHERE(" funeral_dead.deleted = 0 and family_member.deleted = 0 ");

                // 业务编码
                if (valid(data.getBusinessCode())) {
                    WHERE(" funeral_dead.business_code like concat('%',#{data.businessCode},'%') ");
                }

                // 逝者姓名
                if (valid(data.getDeadName())) {
                    WHERE(" funeral_dead.dead_name like concat('%',#{data.deadName},'%') ");
                }
                // 火化时间
                if (data.getAppointmentDate() != null) {
                    WHERE(" DATE(cremation.appointment_date) >= #{data.appointmentDate,jdbcType=DATE} ");
                }


                ORDER_BY("  funeral_dead.business_code desc ");
            }
        }.toString();
    }
}
